package com.stox.ui.swing.widget.wrapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.Action;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered;

import com.stox.ui.swing.util.ImageIconLoader;
import com.stox.ui.swing.util.SwingUtil;
import com.stox.util.ApplicationContextUtil;

public abstract class JMenu extends javax.swing.JMenu implements Ordered, InitializingBean {
	private static final long serialVersionUID = -5609437172102237960L;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		Collection<JMenuItem> jmenuItems = ApplicationContextUtil.getApplicationContext().getBeansOfType(JMenuItem.class).values();
		List<JMenuItem> children = new ArrayList<JMenuItem>();
		for(JMenuItem jmenuItem : jmenuItems){
			if(getClass().equals(jmenuItem.getParentClass())){
				children.add(jmenuItem);
			}
		}
		OrderComparator.sort(children);
		for(JMenuItem jmenuItem : children){
			add(jmenuItem);
		}
	}
	
	private void init(){
		SwingUtil.initializeWidget(this);
	}
	
	public JMenu() {
		super();
		init();
	}

	public JMenu(Action a) {
		super(a);
		init();
	}

	public JMenu(String s, boolean b) {
		super(s, b);
		init();
	}

	public JMenu(String s) {
		super(s);
		init();
	}
	
	public JMenu(String name, int mnemonic, String iconPath){
		super(name);
		setMnemonic(mnemonic);
		setIcon(ImageIconLoader.loadImageIcon(iconPath));
	}

}
